package com.lemon.cloud.admin.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.admin.dto.OAuthClientDTO;
import com.lemon.cloud.admin.entity.OAuthClientDetails;
import com.lemon.cloud.admin.service.SysClientDetailsService;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 客户端管理
 *
 * @author HuangDS
 */
@RestController
@RequestMapping("/client")
@Tag(name = "客户端管理接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysClientController {

    @Autowired
    private SysClientDetailsService clientDetailsService;

    /**
     * 分页查询
     * @param page
     * @param details
     * @return
     */
    @Operation(
            summary = "分页查询",description = "分页查询客户端信息",
            responses = {
                @ApiResponse(responseCode = "200",description = "successful operation",content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,schema = @Schema(implementation = IPage.class))},useReturnTypeSchema = true)
                }
            )
    @GetMapping("/page")
    public ResultMsg<IPage<OAuthClientDetails>> page(@ParameterObject Page<OAuthClientDetails> page,
                                                     @ParameterObject OAuthClientDetails details) {
        return ResultMsg.resultSuccess(clientDetailsService.page(page, Wrappers.query(details)));
    }

    /**
     * 通过clientId查询客户端信息
     * @param clientId
     * @return
     */
    @Operation(summary = "通过clientId查询客户端信息",
            parameters = {@Parameter(name = "clientId",description = "客户端id",required = true,in= ParameterIn.PATH)},
            responses = {@ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400", description = "无效参数")}
            )
    @GetMapping("{clientId}")
    public ResultMsg<OAuthClientDetails> getById(@PathVariable("clientId") String clientId) {
        return ResultMsg.resultSuccess(clientDetailsService.getById(clientId));
    }

    /**
     * 新增客户端
     * @param dto
     * @return
     */
    @Operation(summary = "新增客户端",
            responses = {@ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400", description = "无效参数")}
            )
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys_client_add')")
    public ResultMsg<OAuthClientDetails> addOAuthClientDetails(@Parameter @RequestBody @Valid OAuthClientDTO dto) {
        return ResultMsg.resultSuccess(clientDetailsService.add(dto));
    }

    /**
     * 修改客户端配置
     * @param dto
     * @return
     */
    @Operation(summary = "修改客户端配置",
            responses = {@ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400", description = "无效参数")}
            )
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys_client_edit')")
    public ResultMsg<OAuthClientDetails> updateOAuthClientDetails(@Parameter @RequestBody @Valid OAuthClientDTO dto) {
        return ResultMsg.resultSuccess(clientDetailsService.updateDetails(dto));
    }

    /**
     * 通过id删除客户端
     * @param clientId
     * @return
     */
    @Operation(summary = "删除客户端配置",
            parameters = {@Parameter(name = "clientId",description = "客户端id",required = true,in = ParameterIn.PATH)},
            responses = {@ApiResponse(responseCode = "200",description = "data：true,删除成功，否则删除失败"),
                    @ApiResponse(responseCode = "400", description = "无效参数")}
    )
    @DeleteMapping("{clientId}")
    @PreAuthorize("hasAnyAuthority('sys_client_del')")
    public ResultMsg<Boolean> delById(@PathVariable("clientId") String clientId) {
        return ResultMsg.resultSuccess(clientDetailsService.removeById(clientId));
    }


    @Inner(false)
    @GetMapping("/getClientDetailsById/{clientId}")
    public ResultMsg getClientDetailsById(@PathVariable("clientId") String clientId) {
        return ResultMsg.resultSuccess(clientDetailsService.getOne(
                Wrappers.<OAuthClientDetails>lambdaQuery().eq(OAuthClientDetails::getClientId, clientId), false));
    }

    /**
     * 查询全部客户端
     * @return
     */
    @Inner(false)
    @GetMapping("/list")
    public ResultMsg listClients() {
        return ResultMsg.resultSuccess(clientDetailsService.list());
    }
}
